package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xxxx.yoyo.bean.Order;
import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.bean.User;
import com.xxxx.yoyo.service.IOrderService;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.OrderServiceImpl;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;

/**
 * 在订单页面点击“支付”按钮
 * Servlet implementation class ToPayServlet
 */
@WebServlet("/index/topay")
public class ToPayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ITypeService typeService = new TypeServiceImpl();
	private IOrderService orderService = new OrderServiceImpl();
	
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取session
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null) {//用户没有登录或者session超时失效了
			//重定向到登录页面
			response.sendRedirect("/YoyoShop/index/login");
		}else {
			//获取前端传递过来的参数orderid
			String orderIdStr = request.getParameter("orderid");
			int orderId = Integer.parseInt(orderIdStr);
			
			//获取 [宠物分类] 列表
			List<Type> typeList = typeService.getTypeList();
			//把typeList存入request中,供/index/order.jsp使用
			request.setAttribute("typeList", typeList);
			
			//根据订单id查询订单
			Order order = orderService.getOrderById(orderId);
			//把订单存入request中,供/index/pay.jsp使用
			request.setAttribute("order", order);
			
			//请求转发进入到支付页面/index/pay.jsp
			request.getRequestDispatcher("/index/pay.jsp").forward(request, response);
		}
	}

}
